package com.yanq.liancar.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.bean.BDefaultMailAddressBean;
import com.yanq.liancar.bean.GoodsDetailesBean;
import com.yanq.liancar.bean.MailAddressBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.Utils;

import java.math.BigDecimal;

import butterknife.BindView;

public class SmallBShopOrderActivity extends BaseToolbarActivity {
    @BindView(R.id.img_location)
    android.widget.ImageView imgLocation;
    @BindView(R.id.img_rightgo)
    android.widget.ImageView imgRightgo;
    @BindView(R.id.tv_name)
    android.widget.TextView tvName;
    @BindView(R.id.tv_phoneNum)
    android.widget.TextView tvPhoneNum;
    @BindView(R.id.rl_address)
    android.widget.RelativeLayout rlAddress;
    @BindView(R.id.img_goods)
    android.widget.ImageView imgGoods;
    @BindView(R.id.tv_goodsname)
    android.widget.TextView tvGoodsname;
    @BindView(R.id.tv_goodcontent)
    android.widget.TextView tvGoodcontent;
    @BindView(R.id.tv_goodprice)
    android.widget.TextView tvGoodprice;
    @BindView(R.id.rl_content)
    android.widget.RelativeLayout rlContent;
    @BindView(R.id.iv_sub)
    android.widget.TextView ivSub;
    @BindView(R.id.tv_show_num)
    android.widget.TextView tvShowNum;
    @BindView(R.id.iv_add)
    android.widget.TextView ivAdd;
    @BindView(R.id.rl_buynum)
    android.widget.RelativeLayout rlBuynum;
    @BindView(R.id.et_remarks)
    android.widget.EditText etRemarks;
    @BindView(R.id.rl_remarks)
    android.widget.LinearLayout rlRemarklls;
    @BindView(R.id.tv_num)
    android.widget.TextView tvNum;
    @BindView(R.id.tv_price2)
    android.widget.TextView tvPrice2;
    @BindView(R.id.tv_heji)
    android.widget.TextView tvHeji;
    @BindView(R.id.tv_price3)
    android.widget.TextView tvPrice3;
    @BindView(R.id.tv_buynow)
    android.widget.TextView tvBuynow;
    @BindView(R.id.tv_address)
    android.widget.TextView tvAddress;
    @BindView(R.id.tv_selectAddress)
    android.widget.TextView mTvSelectAddress;

    @BindView(R.id.rl_content1)
    RelativeLayout mRelativeLayout1;

    private int goodsnum = 1;
    private BigDecimal price ;
    private GoodsDetailesBean.DataBean mDataBean;
    private String addressID;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_small_b_shoporder);
        Utils.setStatusBar(this, true, false);
        butterknife.ButterKnife.bind(this);
        mTvTitle.setText("商城订单支付");

        mDataBean = (GoodsDetailesBean.DataBean) getIntent().getExtras().getSerializable("DataBean");
        RequestOptions options=new RequestOptions();
        options.placeholder(R.mipmap.item_img);
        Glide.with(this).load(mDataBean.getGoods_photo()).apply(options).into(imgGoods);
        tvGoodsname.setText(mDataBean.getGoods_name());
        tvGoodcontent.setText(mDataBean.getGoods_introduction());
        tvGoodprice.setText("¥"+mDataBean.getSale_price());
        price=new BigDecimal(mDataBean.getSale_price());
        initTextView();
        getAddress();
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @butterknife.OnClick({R.id.iv_sub, R.id.iv_add, R.id.tv_buynow,R.id.img_rightgo,R.id.tv_selectAddress,R.id.rl_content1})
    public void onViewClicked(android.view.View view) {
        switch (view.getId()) {
            case R.id.iv_sub:
                if (goodsnum > 1) {
                    goodsnum--;
                    initTextView();
                }
                break;
            case R.id.iv_add:
                goodsnum++;
                initTextView();
                break;
            case R.id.tv_buynow:
                BigDecimal bigDecimal=new BigDecimal(goodsnum);
                Bundle bundle = new Bundle();
                bundle.putString("price", "¥" + bigDecimal.multiply(price));
                bundle.putString("goodID", mDataBean.getGoodsid()+"");
                bundle.putString("goodname", mDataBean.getGoods_name());
                bundle.putString("addressID", addressID);
                bundle.putString("goodNum", goodsnum+"");
                bundle.putString("buyers_note", etRemarks.getText().toString().trim());
                RxActivityTool.skipActivity(this, SmallBPaymentActivity.class, bundle);
                break;
            case R.id.tv_selectAddress:
            case R.id.rl_content1:
            case R.id.img_rightgo:
                Bundle bundle1 =new Bundle();
                bundle1.putString("where","SmallBShopOrderActivity");
                RxActivityTool.skipActivityForResult(this, MailAddressActivity.class, bundle1,001);
                break;
        }
    }

    private void initTextView() {
        tvShowNum.setText(goodsnum + "");
        tvNum.setText(goodsnum + "");
        BigDecimal bigDecimal=new BigDecimal(goodsnum);
        tvPrice2.setText("¥" + bigDecimal.multiply(price) );
        tvPrice3.setText("¥" + bigDecimal.multiply(price));
        tvGoodprice.setText("¥"+price);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode==RESULT_OK){
            if (requestCode==001){
                mTvSelectAddress.setVisibility(View.GONE);
                mRelativeLayout1.setVisibility(View.VISIBLE);
                tvName.setText(data.getStringExtra("name"));
                tvPhoneNum.setText(data.getStringExtra("phone"));
                tvAddress.setText(data.getStringExtra("address"));
                addressID=data.getStringExtra("addressid");
            }
        }
    }

    //获取默认地址
    private void getAddress() {
        OkGo.<String>post(Config.s + "llc/api/selectDefaultAddress")
                .tag(this)
                .params("token", UserBean.getbToken(this))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        BDefaultMailAddressBean addressBean = gson.fromJson(response.body(), BDefaultMailAddressBean.class);
                        if (addressBean.isIsSuccess()) {
                            mTvSelectAddress.setVisibility(View.GONE);
                            mRelativeLayout1.setVisibility(View.VISIBLE);
                            tvName.setText(addressBean.getData().getGainpeo());
                            tvPhoneNum.setText(addressBean.getData().getCont_phone());
                            tvAddress.setText(addressBean.getData().getGainaddress()+addressBean.getData().getAddr_detail());
                            addressID=addressBean.getData().getId()+"";
                        }else {
                            RxToast.error(addressBean.getOutputMessage());
                            mTvSelectAddress.setVisibility(View.VISIBLE);
                            mRelativeLayout1.setVisibility(View.GONE);
                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        RxToast.error("服务器连接错误");
                    }
                });
    }
}
